// pages/setuppersonaldata/setuppersonaldata.js
const app = getApp();
var ea = require('../../utils/each.js');
// import WeCropper from '../we-cropper/we-cropper.js'

// const device = wx.getSystemInfoSync() // 获取设备信息
// const width = device.windowWidth // 示例为一个与屏幕等宽的正方形裁剪框
// const height = width
Page({

  /**
   * 页面的初始数据
   */
  data: {

    src: '../../assets/new.png',
    width: 250,//宽度
    height: 250,//高度

    // cropperOpt: {
    //   id: 'cropper', // 用于手势操作的canvas组件标识符
    //   targetId: 'targetCropper', // 用于用于生成截图的canvas组件标识符
    //   pixelRatio: device.pixelRatio, // 传入设备像素比
    //   width,  // 画布宽度
    //   height, // 画布高度
    //   scale: 2.5, // 最大缩放倍数
    //   zoom: 8, // 缩放系数
    //   cut: {
    //     x: (width - 200) / 2, // 裁剪框x轴起点
    //     y: (width - 200) / 2, // 裁剪框y轴期起点
    //     width: 200, // 裁剪框宽度
    //     height: 200 // 裁剪框高度
    //   }
    // },



    rightarrow: app.data.imgurl +"rightarrow.png",
    hedsrc: app.data.imgurl +"headimg.png",
    index:0,
    array: ["男","女"],
    region: ['广东省', '广州市', '海珠区'],
    customItem:'全部',
    getuserinfo: app.data.url + 'sheyingjia/Xmyuser1/getuserinfos',
    gettoken: app.data.url + 'sheyingjia/Xuploas1/gettoken',
    addenclosure: app.data.url + 'sheyingjia/Xuploas1/addenclosure',
    changeuser: app.data.url + 'sheyingjia/Xmyuser1/changeuser',
    myuser:{
      sex:'男'
    },
    qiequ:false,
  },
  xuanz:function(){
   
  },
   
   //男女选择
  bindPickerChange:function(e){
    // console.log(e);
    var index = e.detail.value;
    var myuser = this.data.myuser;
    if(index==1){
      myuser.sex = '女';
    }else{
      myuser.sex = '男';
    }
    console.log(myuser);
   this.setData({
     index: index,
     myuser: myuser
   })
  },
  //选择地址
  bindRegionChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value);
    var region = e.detail.value;
    var myuser = this.data.myuser;
    if(region[0]){
      myuser.sheng = region[0];
    }
    if (region[1]) {
      myuser.shi = region[1];
    }
    if (region[2]) {
      myuser.qu = region[2];
    }
    console.log(myuser);
    this.setData({
      region: e.detail.value,
      myuser:myuser
    })
  },
  //修改电话号码
  toupdatephonenumber:function(e){
    console.log(e);
    var phone = e.currentTarget.dataset.value;
    // return;
   wx.navigateTo({
     url: '/pages/updatephonenumber/updatephonenumber?phone='+phone,
   })

  },
  changeinpuf:function(e){
    // console.log(e);
    var myuser = this.data.myuser;
    var type = e.currentTarget.dataset.type;
    myuser[type] = e.detail.value;
    console.log(myuser);
    this.setData({myuser:myuser});
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var user = wx.getStorageSync('user');
    if (!user) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return
    }
    wx.showLoading({
      title: '加载中',
    })
    ea.request(that.data.getuserinfo, { token: user.token }, function (res) {
      wx.hideLoading();
      var data = res.data;
      if (data.code == 200) {
        console.log(data.data);
        var myuser = data.data;
        if (myuser.sex == '男') {
          that.setData({ index: 0 });
          myuser.sex = '男';
        } else {
          that.setData({ index: 1 });
          myuser.sex = '女';
        }
        var region = [];
        if (myuser.sheng){
          region.push(myuser.sheng);
        }else{
          myuser.sheng = '广东省';
        }
        if (myuser.shi) {
          region.push(myuser.shi);
        } else {
          myuser.shi = '广州市';
        }
        if (myuser.qu) {
          region.push(myuser.qu);
        } else {
          myuser.qu= '珠海区';
        }
        if (region.length==0){
          that.setData({ myuser: myuser});
        }else{
          that.setData({ myuser: myuser, region: region });
        }
        
      }else{
        wx.showToast({
          title: data.msg,
          icon: 'none',
          duration: 3000,
        })
      }
    
    })
  },
  
  //保存数据
  baocunzai:function(){
    var myuser = this.data.myuser;
    var user = wx.getStorageSync('user');
    var that = this;
    myuser.token = user.token;
  
    console.log(myuser);
    // return
    ea.request(that.data.changeuser,myuser,function(res){
      var data = res.data;
      console.log(data);
      if(data.code==200){
        wx.showToast({
          title: '保存成功',
          // icon: 'none',
          duration: 2000,
        })
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          })
        }, 2000);
      }else{
        wx.showToast({
          title: data.msg,
          // icon: 'none',
          duration: 2000,
        })
      }
      
    }, '保存中')
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var phone = wx.getStorageSync('myphone');
    if(phone){
      var myuser = this.data.myuser;
      myuser.phone = phone;
      wx.setStorageSync('myphone', '');
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //获取头像的照片
  uploade: function () {
    var that = this;
    wx.chooseImage({
      count: 1,
      // sizeType: [],
      // sourceType: [],
      success: function (res) {
        that.setData({ qiequ: true });
        var src = res.tempFilePaths[0];
        console.log(src);
        that.cropper = that.selectComponent("#image-cropper");
        console.log(that.cropper);
        that.cropper.getImg = function (ress) {
          console.log(ress);
        }
        that.setData({
          src: src,
        });
        wx.showLoading({
          title: '加载中'
        })
        wx.showModal({
          title: '提示',
          content: '选择好后，点击图片中心上传',
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  cropperload(e) {
    console.log("cropper初始化完成");
  },
  loadimage(e) {
    console.log("图片加载完成", e.detail);
    wx.hideLoading();
    //重置图片角度、缩放、位置
    this.cropper.imgReset();
  },
  clickcut(e) {
   console.log(e);
   var myuser = this.data.myuser;
    myuser.touxiangs = e.detail.url;
    this.setData({myuser:myuser,qiequ:false});
    wx.showLoading({
      title: '上传中',
    })
    var that = this;
    ea.upfile([myuser.touxiangs],function(e,key){
      // console.log(e[0]);
      myuser.touxiangs = e[0];
      var name = key[0];
      // return
      ea.request(that.data.addenclosure, { name: name},function(ress){
        wx.hideLoading();
        console.log(ress);
        var data = ress.data;
        var myuser = that.data.myuser;
        myuser.touxiang = data.data.id;
        console.log(myuser);
        that.setData({ myuser: myuser});
        // myuser.touxiang = res.data.data.id;
        // console.log(myuser);
        // this.setData({ myuser: myuser, qiequ: false });
      })
    }, that.data.gettoken,'SCN',function(e){
      var str = e.progress;
      if(str==100){
        str = 99;
      }
      str = str.toString();
      wx.showLoading({
        title: str+"%",
      })
    })
  },
})